﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FluentScript.IDE;
using FluentScript.IDE.Parts;



namespace FluentScript.Silverlight
{
    public partial class MainPage : UserControl, IIdeView
    {
        private IdeController _controller;
        private string _sample =
        "# Dates "
        + Environment.NewLine + "set birthday = January 1st 1979"
        + Environment.NewLine + "if birthday is before 8/15/1991 then print you can vote" + Environment.NewLine
        + Environment.NewLine + "# Day "
        + Environment.NewLine + "if today is not monday then print thank god it's not the start of the work week!" + Environment.NewLine
        + Environment.NewLine + "# Money "
        + Environment.NewLine + "set salary = $250"
        + Environment.NewLine + "if salary is more than $200 then print I worked overtime" + Environment.NewLine
        + Environment.NewLine + "# time "
        + Environment.NewLine + "set time = 3:30 pm"
        + Environment.NewLine + "if time is before 5pm then print still have to work!" + Environment.NewLine
        + Environment.NewLine + "# select"
        + Environment.NewLine + "set books = [  name	      |	 pages  |  artist"
        + Environment.NewLine + "                     'batman'    ,	 110     ,  'john'"
        + Environment.NewLine + "                     'xmen'       ,	 120     ,  'lee'"
        + Environment.NewLine + "                     'daredevil'  ,	 140     ,  'maleev'"
        + Environment.NewLine + "                  ]" + Environment.NewLine
        + Environment.NewLine + "set favorites = books where book.pages > 120"
        + Environment.NewLine + "print( favorites[0].name )" + Environment.NewLine
        + Environment.NewLine + "# url"
        + Environment.NewLine + "set site = www.yahoo.com" + Environment.NewLine
        + Environment.NewLine + "# aggregates "
        + Environment.NewLine + "set numbers = [1, 2, 3, 4]"
        + Environment.NewLine + "print( sum of numbers )";

        public MainPage()
        {            
            InitializeComponent();
            _controller = new IdeController(this);
            _controller.AddViewPart(new Locals(this.locals));
            _controller.AddViewPart(new Output(this.results));
            this.source.Text = _sample;
        }


        /// <summary>
        /// The source code.
        /// </summary>
        public string SourceCode
        {
            get { return this.source.Text; }
        }


        public void SetError(string error)
        {
            ClearErrors();
            this.results.Text = "ERROR: " +  error;
        }


        public void ClearErrors()
        {
            this.results.Text = string.Empty;
        }


        #region Event-handlers
        private void btnClearResults_Click(object sender, RoutedEventArgs e)
        {
            _controller.OnClear();
        }


        private void btnRun_Click(object sender, RoutedEventArgs e)
        {            
            _controller.OnRun();
        }
        

        private void source_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                e.Handled = true;

                var tb = ((TextBox)sender);
                tb.Text += "\t";
                tb.Select(tb.Text.Length, 0);
            }

        }
        #endregion
    }
}
